package event.gui;

import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JComponent;

import main.model.Settings;
import event.model.NarwhalEvent;

@SuppressWarnings("serial")
public class EventSideButton extends JComponent implements MouseListener {

	private EventFrame frame;
	private boolean active;
	private final Color BACKGROUND_ACTIVE = Color.LIGHT_GRAY;
	private final Color BACKGROUND_NO_ACTIVE = Color.DARK_GRAY;
	private final Color TEXT_COLOR = Color.WHITE;
	private int index;
	private NarwhalEvent model;

	public EventSideButton(NarwhalEvent model, EventFrame frame, int index) {
		this.frame = frame;
		this.index = index;
		this.model = model;
		addMouseListener(this);
	}

	public void setActive(boolean a) {
		active = a;
		repaint();
	}

	public int getIndex() {
		return index;
	}

	@Override
	public void paint(Graphics g) {
		int width = getBounds().width;
		int height = getBounds().height;

		Color bg = BACKGROUND_NO_ACTIVE;
		if (active)
			bg = BACKGROUND_ACTIVE;
		g.setColor(bg);
		g.fillRect(0, 0, width, height);
		g.setColor(TEXT_COLOR);
		FontMetrics fm = g.getFontMetrics();
		int x = 10;
		int y = getBounds().height
				- ((getBounds().height - fm.getHeight()) / 2);
		g.drawString(model.getTitle(index), x, y);
	}
	
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof EventSideButton)
			return index == ((EventSideButton) obj).index;
		return false;
	}

	private void click() {
		if (getParent().isEnabled()) {
			frame.setActive(this);
			frame.moveMouse();
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		if (new Settings().clickToClick())
			click();
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		if (new Settings().enterToClick())
			click();
	}

	@Override
	public void mouseExited(MouseEvent e) {
	}

	@Override
	public void mousePressed(MouseEvent e) {
	}

	@Override
	public void mouseReleased(MouseEvent e) {
	}

}
